//Code reused from Threeve Corp.
package DTI;

public class CharacterAnalyzer {
	
	  /**
	   * The first invalid character.
	   */
	  static final int CHARS_END = 256;
	  /**
	   * The first non-break character.
	   */
	  static final int NONBREAK_BEGIN = 32;
	  /**
	   * The first break character after all non-break characters.
	   */
	  static final int NONBREAK_END = 127;

	  /**
	   * Stores whether each character from 0 to 255 is or is not a break character.
	   */
	  private static boolean break_characters_[];

	  /**
	   * Constructs a character analyzer with the specified set of break characters.
	   * The specified set is either the default set or a user-defined set of breakers.
	   * 
	   * @param breakChars the set of unique characters, each of which must be in
	   *          the range 32 to 126, which comprise the variable break characters.
	   * @throws IllegalArgumentException if any character in
	   *           <code>breakChars</code> is less than 32 or greater than 126; or
	   *           if any character appears in <code>breakChars</code> more than
	   *           once.
	   */
	  public CharacterAnalyzer(String break_chars) {
		  break_characters_ = computeBreakCharacters(break_chars);
	  }
	
	  /**
	   * Returns whether or not the specified character is a break character.
	   * 
	   * @param ch the character in question, must be in the range 0 to 255
	   * @return <code>true</code> if <code>ch</code> is a break character;
	   *         otherwise <code>false</code>
	   * @throws ArrayIndexOutOfBoundsException if <code>ch</code> < 0 or
	   *           <code>ch</code> > 255
	   */
	  public static boolean isBreakCharacter(char ch) {
	    return break_characters_[ch];
	  }

	  /**
	   * Computes the set of break and non-break characters, based on the passed-in
	   * set of variable break characters. Characters in the range 0 to 31 and 127
	   * to 255 (inclusive) are always break characters. Characters in the range 32
	   * to 126 are break characters if they are contained in
	   * <code>break_chars</code>.
	   * 
	   * @param break_chars the set of unique characters, each of which must be in
	   *          the range 32 to 126, which comprise the variable break characters.
	   * @return an 256 element array, in which the element corresponding to each
	   *         character between 0 and 255 is true the character is a break
	   *         character and false otherwise.
	   * @throws IllegalArgumentException if any character in
	   *           <code>break_chars</code> is less than 32 or greater than 126; or
	   *           if any character appears in <code>break_chars</code> more than
	   *           once.
	   */
	  private static boolean[] computeBreakCharacters(String break_chars) {
	    boolean[] result = new boolean[CHARS_END];

	    // Initialize characters from 0 to 31 and 127 to 255 as break characters.
	    for (int i = 0; i < NONBREAK_BEGIN; ++i) {
	      result[i] = true;
	    }
	    for (int i = NONBREAK_END; i < CHARS_END; ++i) {
	      result[i] = true;
	    }

	    // Initialize the specified variable break characters as break characters.
	    char[] break_chars_array = break_chars.toCharArray();
	    for (int i = 0; i < break_chars_array.length; ++i) {
	      char c = break_chars_array[i];
	      if (result[c]) {
	        // Either a fixed character, or already saw this character.
	        throw new IllegalArgumentException("'" + c
	          + "' is already a break character");
	      }
	      result[c] = true;
	    }

	    return result;
	  }

}